<?php
session_start();
require_once('../model/db.php');
if (isset($_GET['func'])) {
	$result = call_user_func($_GET['func'], $_POST);
}
	
function gameInfo($params) {
	$game_id =  $params['game_id'];

	$result = select_from_db(array('password,name,start_time,round_time,map'),
							 array('games'),
							 array("games.id = $game_id"));

	if ($result[0]['password']) {
		$result[0]['password']=1;
	} else {
		$result[0]['password']=0;
	} 
	echo json_encode($result);
}

function availableCountrys($params) {
	$game_id =  $params['game_id'];

	$result = select_from_db(array('russia, italy, germany, france, austria, england, turkey'),
							 array('games'),
							 array("games.id = $game_id"));

	if ($result[0]['russia']) {
		$user_id = $result[0]['russia'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['russia'] = "Russia - ".$user_alias[0]['alias'];
		
	} 
	if ($result[0]['italy']) {
		$user_id = $result[0]['italy'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['italy'] = "Italy - ".$user_alias[0]['alias'];
		
	} 
	if ($result[0]['germany']) {
		$user_id = $result[0]['germany'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['germany'] = "Germany - ".$user_alias[0]['alias'];
		
	} 
	if ($result[0]['france']) {
		$user_id = $result[0]['france'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['france'] = "France - ".$user_alias[0]['alias'];
		
	} 
	if ($result[0]['austria']) {
		$user_id = $result[0]['austria'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['austria'] = "Austria - ".$user_alias[0]['alias'];
		
	} 
	if ($result[0]['england']) {
		$user_id = $result[0]['england'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['england'] = "England - ".$user_alias[0]['alias'];
	} 
	if ($result[0]['turkey']) {
		$user_id = $result[0]['turkey'];
		$user_alias = select_from_db(array('alias'), array('users'), array("id = $user_id"));
		$result[0]['turkey'] = "Turkey - ".$user_alias[0]['alias'];
	}
	echo json_encode($result);
}
//users__got__games.game_id =5
//AND users__got__games.user_id = users.id
//AND army.game_id = users__got__games.game_id
//AND army.user_id = users__got__games.user_id
function gameUserList($params) {
	$game_id =  $params['game_id'];

	$userstats = select_from_db(array('users.id', 'users.alias', 'users__got__games.land', 'count( army.nation ) AS nations'),
	array('users','users__got__games','army'),
	array("users__got__games.game_id = $game_id",
		'users__got__games.user_id = users.id',
		'army.game_id = users__got__games.game_id', 
		'army.user_id = users__got__games.user_id GROUP BY users.id'));
	
//	$userarray = select_from_db(array('users.id','users.alias','users__got__games.land', ',array('users','users__got__games'),array("users__got__games.game_id = $game_id",'users__got__games.user_id = users.id'));
//	$armyarray = select_from_db(array('count(nation), user_id'), "army", "game_id=$game_id GROUP BY user_id");
	
	echo json_encode($userstats);
}

?>